<?php
namespace app\index\model;
use think\Model;
use think\Db; 
use think\Request;
use think\Cookie;
use app\common\service\SessionUser;
use app\index\model\traits\ModelHasTime;


class KnowUser extends Model
{
	use ModelHasTime;


	public function updateUser($phone,$status = 'pc',$typestatus = ''){
		$data = self::getUserOne($phone);
		if(!$data) return [];
		$token = getLoginToken();
		$arr = array(
			'login_time'=>date('Y-m-d H:i:s',time()),
			'login_ip' => get_client_ip(),
			'login_num' => $data['login_num']+1,
		);
		$arrUser = array(
			'id' => $data['know_user_id'],
			'phone' => $phone,
			'token' => $token,
		);
		SessionUser::login($arrUser, $token);
		self::where(['know_user_id'=>$data['know_user_id']])->update($arr);
		$data['token'] = $token;
		return $data;
	}

	public function updateUserPass($params,$status = 'pc',$typestatus = ''){
		$data = self::getUserOne($params['phone']);
		if(!$data) return [];
		if ($data['know_password'] != md5(md5($params['password']).$data['salt'])) return [];
		$token = getLoginToken();
		$arr = array(
			'login_time'=>date('Y-m-d H:i:s',time()),
			'login_ip' => get_client_ip(),
			'login_num' => $data['login_num']+1,
		);
		$arrUser = array(
			'id' => $data['know_user_id'],
			'phone' => $params['phone'],
			'token' => $token,
		);
		SessionUser::login($arrUser, $token);
		self::where(['know_user_id'=>$data['know_user_id']])->update($arr);
		$data['token'] = $token;
		return $data;
	}

	public function insertUser($arr,$type = 'phone',$status = 'pc'){
		$data = self::getUserOne($arr[$type]);
		if($data) return ['code'=>400,'know_user_id'=>$data['know_user_id']];
		if(!checkNicknameRight($arr[$type])) return ['code'=>401,'know_user_id'=>$data['know_user_id']];
		$salt = random(6);
		$salt = self::checksalt($salt);
		$token = getLoginToken();
		$arrIns = array(
			'know_user_id' => create_uuid(),
			'know_'.$type => $arr[$type],
			'know_password' => md5(md5($arr['password']).$salt),
			'register_time' => date('Y-m-d H:i:s',time()),
			'register_ip' => get_client_ip(),
			'salt' => $salt,
			'token' => $token,
		);
		$model = new KnowUser();
		if($model->create($arrIns)){
			$arrUser = array(
				'id' => $arrIns['know_user_id'],
				'phone' => $arr[$type],
			);
			if($status == 'pc') SessionUser::login($arrUser, $token);
		}
		return $arrIns;
	}

	public function changePass($params)
	{
		$data = self::getUserOne($params['phone']);
		if(!$data) return [];
		$password = md5(md5($params['password']).$data['salt']);
		self::where('know_user_id', $data['know_user_id'])->update([
			'know_password' => $password,
		]);
		return $data;
	}

	public function getUserOne($phone){
		if(checkNicknameRight($phone)) $phone = encrypts($phone,config('sha256'));
		$data = self::field('know_user_id,know_phone,know_email,know_nickname,know_desc,know_password,salt,know_avatar,login_num')
						->where(['know_phone|know_email'=>$phone])->find();
		return $data;
	}

	public function getUserOneIds($id,$field = '*'){
		$data = self::field($field)->where('know_user_id',$id)->find();
		return $data;
	}


	public function checksalt($salt){
		$data = self::where('salt',$salt)->find();
		if($data){
			$salt = random(6);
			$this->checksalt($salt);
		}else{
			return $salt;
		}
	}

	public function getDataOne($field,$value){
		if(checkNicknameRight($value)) $value = encrypts($value,config('sha256'));
		return self::where($field, $value)->find();
	}
}